#!/bin/bash

HOST=http://localhost
MONGO_URL=mongodb://localhost:27017/rocketchat
MONGO_OPLOG_URL=mongodb://localhost:27017/local
ROOT_URL=http://localhost
PORT=3000

ROOTPATH=/var/www/fans12306_2

cd $ROOTPATH
meteor build --server "$HOST" --directory $ROOTPATH

cd $ROOTPATH/bundle/programs/server
npm install
rm -f pm2-rocket-chat.json
echo '{'                                                     > pm2-rocket-chat.json
echo '  "apps": [{'                                         >> pm2-rocket-chat.json
echo '    "name": "rocket.chat",'                           >> pm2-rocket-chat.json
echo '    "script": "/var/www/fans12306_2/bundle/main.js",' >> pm2-rocket-chat.json
echo '    "out_file": "/var/log/rocket.chat/app.log",'      >> pm2-rocket-chat.json
echo '    "error_file": "/var/log/rocket.chat/err.log",'    >> pm2-rocket-chat.json
echo "    \"port\": \"$PORT\","                             >> pm2-rocket-chat.json
echo '    "env": {'                                         >> pm2-rocket-chat.json
echo "      \"MONGO_URL\": \"$MONGO_URL\","                 >> pm2-rocket-chat.json
echo "      \"MONGO_OPLOG_URL\": \"$MONGO_OPLOG_URL\","     >> pm2-rocket-chat.json
echo "      \"ROOT_URL\": \"$ROOT_URL\","                   >> pm2-rocket-chat.json
echo "      \"PORT\": \"$PORT\""                            >> pm2-rocket-chat.json
echo '    }'                                                >> pm2-rocket-chat.json
echo '  }]'                                                 >> pm2-rocket-chat.json
echo '}'                                                    >> pm2-rocket-chat.json

pm2 startOrRestart pm2-rocket-chat.json
pm2 save

